package cn.smbms.controller;

import java.io.UnsupportedEncodingException;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.smbms.bean.model.Staff;
import cn.smbms.bean.model.SysRole;
import cn.smbms.bean.model.UserRole;
import cn.smbms.bean.vo.SysRoleVo;
import cn.smbms.bean.vo.UserRoleVo;
import cn.smbms.biz.ISysRoleBiz;
import cn.smbms.biz.IUserRoleBiz;

@Controller
@RequestMapping("/sysRole")
public class SysRoleController {
	@Resource(name = "userRoleBiz")
	private IUserRoleBiz userRoleBiz;
	@Resource(name = "sysRoleBiz")
	private ISysRoleBiz sysRoleBiz;
	
	List<SysRole> list = null;
	
	@RequestMapping("/get")//获取所有角色信息
	public String get(HttpServletRequest request,SysRoleVo sysRoleVo,Model model){	
		try {
			list = sysRoleBiz.getSysRoleBySysRoleVo(sysRoleVo);
			int totalPages = sysRoleBiz.getTotalPages(sysRoleVo).getTotalPages();
			model.addAttribute("list",list);
			model.addAttribute("sysRoleVo",sysRoleVo);
			model.addAttribute("totalPages",totalPages);	
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "sysRole/index";
	}
	
	@RequestMapping("/delete1")//执行删除方法
	public String delete(HttpServletRequest request,@RequestParam(value="roleId")Integer roleId,SysRoleVo sysRoleVo,Model model) throws UnsupportedEncodingException{	
		request.setCharacterEncoding("utf-8");
		int result = sysRoleBiz.delete(roleId);
		if(result > 0) {
			model.addAttribute("msg", "删除成功！");
		}else{
			model.addAttribute("msg", "删除失败！");
		}
		return "redirect:/sysRole/get/";
	}
	
	@RequestMapping("/delete")//进入删除页面
	public String delete(HttpServletRequest request, SysRoleVo sysRoleVo,Integer roleId,Model model){
		Staff staff = (Staff) request.getSession().getAttribute("staff");
		UserRoleVo vo = new UserRoleVo();
		vo.setChnName(staff.getChnName());
		List<UserRole> userRoles = userRoleBiz.getUserRoleByUserRoleVo(vo);
		boolean isAdmin = false;//是否有管理角色权限
		for (UserRole userRole : userRoles) {
			SysRole sysRole = sysRoleBiz.findSysRoleByRoleId(userRole.getRoleId());
			if (sysRole.getManagerRoleFlag() == 1) {
				isAdmin = true;
				break;
			}
		}
		if (isAdmin) {
			//调用业务方法
			list = sysRoleBiz.getSysRoleBySysRoleVo(sysRoleVo);
			//把sysroleList添加到model中，相当于把userList添加到request作用域
			model.addAttribute("list",list);
			int totalPages = sysRoleBiz.getTotalPages(sysRoleVo).getTotalPages();
			model.addAttribute("totalPages",totalPages);
			SysRole info = sysRoleBiz.findSysRoleByRoleId(roleId);
			model.addAttribute("info", info);
			return "sysRole/delete";
		} else {
			return "forward:get?msg=权限不足";
		}
	}
	
	@RequestMapping("/addshow")
	public String addshow(SysRoleVo sysRoleVo,Integer roleId,Model model){
		try {
			//调用业务方法
			list = sysRoleBiz.getSysRoleBySysRoleVo(sysRoleVo);
			//把sysroleList添加到model中，相当于把userList添加到request作用域
			model.addAttribute("list",list);
			SysRole info = sysRoleBiz.findSysRoleByRoleId(roleId);
			model.addAttribute("info", info);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "sysRole/add";
	}
	
	@RequestMapping("/addPage")//进入添加角色页面
	public String addPage(HttpServletRequest request,Model model){	
		Staff staff = (Staff) request.getSession().getAttribute("staff");
		UserRoleVo vo = new UserRoleVo();
		vo.setChnName(staff.getChnName());
		List<UserRole> userRoles = userRoleBiz.getUserRoleByUserRoleVo(vo);
		boolean isSuperAdmin = false;
		for (UserRole userRole : userRoles) {
			SysRole sysRole = sysRoleBiz.findSysRoleByRoleId(userRole.getRoleId());
			if (sysRole.getManagerRoleFlag() == 1) {
				isSuperAdmin = true;
				break;
			}
		}
		if (isSuperAdmin) {
			return "sysRole/add";
		} else {
			return "forward:get?msg=权限不足";
		}
	}
	
	@RequestMapping("/add")	//执行添加角色信息操作
	public String add(HttpServletRequest request,Model model) throws UnsupportedEncodingException{
		request.setCharacterEncoding("utf-8");
		String name = request.getParameter("name");
		//int deleteFlag = Integer.parseInt(request.getParameter("deleteFlag"));
		int managerRoleFlag = Integer.parseInt(request.getParameter("managerRoleFlag"));
		SysRole role = new SysRole();
		role.setName(name);
		//role.setDeleteFlag(deleteFlag);
		role.setManagerRoleFlag(managerRoleFlag);
		if(sysRoleBiz.findSysRolefByName(name) == null){
			int result = sysRoleBiz.add(role);
			if(result > 0){
				model.addAttribute("msg", "添加成功！");
				return "redirect:/sysRole/get";
			}else{
				model.addAttribute("msg", "添加失败！");
				return "sysRole/add";
			}
		}else{
			model.addAttribute("msg","该角色名称已存在！");
			return "sysRole/add";
		}
	}
	
	@RequestMapping("/modify1")	//进入修改页面
	public String modify1(HttpServletRequest request, SysRoleVo sysRoleVo,Integer roleId,Model model){
		Staff staff = (Staff) request.getSession().getAttribute("staff");
		UserRoleVo vo = new UserRoleVo();
		vo.setChnName(staff.getChnName());
		List<UserRole> userRoles = userRoleBiz.getUserRoleByUserRoleVo(vo);
		boolean isSuperAdmin = false;
		for (UserRole userRole : userRoles) {
			SysRole sysRole = sysRoleBiz.findSysRoleByRoleId(userRole.getRoleId());
			if (sysRole.getManagerRoleFlag() == 1) {
				isSuperAdmin = true;
				break;
			}
		}
		if (isSuperAdmin) {
			//调用业务方法
			list = sysRoleBiz.getSysRoleBySysRoleVo(sysRoleVo);
			//把sysroleList添加到model中，相当于把userList添加到request作用域
			model.addAttribute("list",list);
			int totalPages = sysRoleBiz.getTotalPages(sysRoleVo).getTotalPages();
			model.addAttribute("totalPages",totalPages);
			SysRole into = sysRoleBiz.findSysRoleByRoleId(roleId);
			model.addAttribute("into",into);
			return "sysRole/modify1";
		} else {
			return "forward:get?msg=权限不足";
		}
	}
	
	@RequestMapping("/modify")
	public String detail(HttpServletRequest request, SysRoleVo sysRoleVo,Integer roleId,Model model){
		Staff staff = (Staff) request.getSession().getAttribute("staff");
		UserRoleVo vo = new UserRoleVo();
		vo.setChnName(staff.getChnName());
		List<UserRole> userRoles = userRoleBiz.getUserRoleByUserRoleVo(vo);
		boolean isSuperAdmin = false;
		for (UserRole userRole : userRoles) {
			SysRole sysRole = sysRoleBiz.findSysRoleByRoleId(userRole.getRoleId());
			if (sysRole.getManagerRoleFlag() == 1) {
				isSuperAdmin = true;
				break;
			}
		}
		if (isSuperAdmin) {
			//调用业务方法
			list = sysRoleBiz.getSysRoleBySysRoleVo(sysRoleVo);
			//把sysroleList添加到model中，相当于把userList添加到request作用域
			model.addAttribute("list",list);
			int totalPages = sysRoleBiz.getTotalPages(sysRoleVo).getTotalPages();
			model.addAttribute("totalPages",totalPages);
			SysRole into = sysRoleBiz.findSysRoleByRoleId(roleId);
			model.addAttribute("into",into);
			return "sysRole/modify";
		} else {
			return "forward:get?msg=权限不足";
		}
	}
	
/*	@RequestMapping("/modifyPage")
	public String modifyPage(@RequestParam(value="roleId")Integer roleId,Model model){	//进入修改用户信息页面
		SysRole role = dao.findSysRoleByRoleId(roleId);
		model.addAttribute("role",role);
		return "/sysRole/modify";
	}*/
	
	@RequestMapping("/update")//执行修改方法
	public String update(HttpServletRequest request,Model model) throws UnsupportedEncodingException{
		request.setCharacterEncoding("utf-8");
		SysRole role = sysRoleBiz.findSysRoleByRoleId(Integer.parseInt(request.getParameter("roleId")));
		String name = request.getParameter("name");
		//int deleteFlag = Integer.parseInt(request.getParameter("deleteFlag"));
		int managerRoleFlag = Integer.parseInt(request.getParameter("managerRoleFlag"));
		role.setName(name);
		//role.setDeleteFlag(deleteFlag);
		role.setManagerRoleFlag(managerRoleFlag);
		int result = sysRoleBiz.update(role);
		if(result > 0){
			model.addAttribute("msg", "修改成功！");
			return "redirect:/sysRole/get";
		}else{
			model.addAttribute("msg","修改失败！");
			return "sysRole/modify";
		}
	}
	
	//批量删除
	@RequestMapping(value = "/ajax", produces = { "application/json;charset=UTF-8" })
	@ResponseBody
	public List<SysRole> ajax(String id) {
		System.out.println(id);
		List<SysRole> list = null;
		String[] sourceStrArray = id.split(",");
		System.out.println("sourceStrArray : " + sourceStrArray.length);
		int cg = 0;
		for (int i = 0; i < sourceStrArray.length; i++) {
			try {
				sysRoleBiz.delete(Integer.parseInt(sourceStrArray[i]));
			} catch (Exception e) {
				e.printStackTrace();
			}
			System.out.println(sourceStrArray[i]);
		}
		return list;
	}
}